<?php
/**
 * ZendFormUtil
 *
 * @author fusq
 */

class ZendFormUtil {

       public static function renderElement($element,$attributes = null,$message = ""){
           $separator = "";
           $elementLabel = "";
           $elementHtml = "";
           $elementError = "";

           $attr = "";
           if(isset($attributes) && !empty($attributes)){
               foreach($attributes as $key => $value){
                    $attr .= " $key" . "='$value'";
               }
           }
           $elementLabel .= ZendFormUtil::renderLabel($element,$separator,$attr);

           $errors = $element->getMessages();
           $id = $element->getAttrib("id");
           $elementError = '';
           $elementError .= "<div class='form_line_div'>";//add
           if(!empty($id)){
               $elementError .= "<div class='form_element_label' $attr id='". $id ."ErrorLabel'>";//add
           }else{
               $elementError .= "<div class='form_element_label' $attr>";//add
           }
           $elementError .= empty($errors)? "" : "&nbsp;";
           $elementError .= "</div>";//add
           if(!empty($id)){
               $elementError .= "<div class='form_element_error' id='". $id ."Error'>";
           }else{
               $elementError .= "<div class='form_element_error'>";
           }
           $elementError .= empty($errors)? "" : array_shift($errors);
           $elementError .= "</div>";
           $elementError .= "</div>";//add
           $elementError .= "<div class='clear'></div>";//add
           


           $elementHtml .="<div class='form_line_div'>";
           $elementHtml .= $elementLabel;
           $elementHtml .= $element->render();
           if(!empty($message)){
               $elementHtml .= $message;
           }
           //$elementHtml .= $elementError;
           $elementHtml .= "</div>";           
           $elementHtml .= "<div class='clear'></div>";
           $elementHtml .= $elementError;//add

           echo $elementHtml;
       }

       public static function renderLabel($element,$separator,$attr){
           $elementLabel = '';
           $elementLabel .= "<div class='form_element_label' ". $attr .">";
           $elementLabel .=  $element->getLabel() . $separator;
           $elementLabel .= "</div>";
           return $elementLabel;
       }
       
       public static function renderErrorMessage($element){
           $errorHtml = "";
           $errors = $element->getMessages();
           if(!empty($errors)){
               $errorHtml = array_shift($errors);
           }
           return $errorHtml;
       }

       public static function aaMenu($resource,$privilege,$href,$label){
           $allow = AAService::isAllowed($resource, $privilege);
           if($allow){
              return "<li><a href='$href'>$label</a></li>";
           }else{
               return "";
           }
       }

       public static function renderHidden($element){
           $name = $element->getName();
           $value = $element->getValue();
           $elementHtml = "<input id='$name' type='hidden' value='$value' name='$name'/>";
           return $elementHtml;
       }

       public static function renderHiddenElement($element){
           $name = $element->getName();
           $value = $element->getValue();
           $elementHtml = "<input id='$name' type='hidden' value='$value' name='$name'/>";
           echo $elementHtml;
       }
       
       public static function renderTextElement($element,$attributes = null,$message = ""){
           $eid = $element->getId();
           $html = "<div class='form_line_div'>";
           $html .= ZendFormUtil::renderLabel($element, $attributes, $message);
           $html .= "<div class=\"form_element\">";
           $html .= "<input type=\"text\" value=\"{$element->getValue()}\" id=\"{$eid}\" name=\"{$eid}\"/>";
           $html .= "</div>";
           if (!empty($eid)){
               $html .= "<div class='form_element_error' id='{$eid}Error'>";
           }
           else{
               $html .= "<div class='form_element_error'>";
           }                  
           $errors = $element->getMessages();                      
           $html .= empty($errors)? "" : array_shift($errors);
           $html .= "</div>";
           $html .= "</div>";
           $html .= "<div class='clear'></div>";
           echo $html; 
       }
       
       public static function renderRadioElement($element,$attributes = null,$message = ""){
           $eid = $element->getId();
           $html = "<div class='form_line_div'>";
           $html .= ZendFormUtil::renderLabel($element, $attributes, $message);
           $html .= "<div class=\"form_element\">";
           $options = $element->getMultiOptions();
           $value = $element->getValue();
           
           foreach ($options as $key => $option)
           {
               $checked = $key == $value ? "checked": "";
               $html .= "<input type='radio' name='$eid' id='{$eid}[$key]' value='$key' $checked>$option&nbsp;";
           }
           
           $html .= "</div>";
           if (!empty($eid)){
               $html .= "<div class='form_element_error' id='{$eid}Error'>";
           }
           else{
               $html .= "<div class='form_element_error'>";
           }                  
           $errors = $element->getMessages();                      
           $html .= empty($errors)? "" : array_shift($errors);
           $html .= "</div>";
           $html .= "</div>";
           $html .= "<div class='clear'></div>";
           echo $html; 
       }
}
?>
